﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DentScan2.WebApp
{
    public partial class Login : Classes.BaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitAreas();
            }
        }

        private void InitAreas()
        {
            ddlArea.DataSource = dl.Areas.Where(area => !area.IsDel).OrderBy(area => area.AreaName);
            ddlArea.DataBind();
        }

        protected void bLogin_Click(object sender, EventArgs e)
        {

            


            var logins = dl.Users.Where(user => (!user.IsDel) && user.UserName == tbUserName.Text && user.Password == Tools.Tools.GetMd5Hash(tbPassword.Text)).OrderBy(user=>user.UserName);
            DB.User login = logins.FirstOrDefault();

            if (login == null)
            {
                lError.Visible = true;
            }
            else
            {
                this.UserId = login.UserId;
                this.AreaId = Guid.Parse(ddlArea.SelectedValue);
                this.UserName = login.UserName;
                this.FullName = login.FullName;
                this.Phone = login.Phone;
                this.Level = login.LevelId;                
                Response.Redirect("FindCar.aspx?NewDamage=1");
                Response.End();

            }
        }

        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}